using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Net;
using System.IO;
using System.Xml;
namespace ContactsTester.BoxConnector
{
    class BoxConnector
    {
        public BoxConnector()
        {
            AuthenticateAndUpload("dummy");
        }
        private string apiKey;

        public string ApiKey
        {
            get { return "jxeh5xsex3gcepvf2mgognm8l5cc2sik"; }
        }

        public void AuthenticateAndUpload(string path)
        {
            System.Uri uri = new Uri("http://www.box.net/api/1.0/rest?action=get_ticket&api_key="+ ApiKey);
            WebRequest webRequest = HttpWebRequest.CreateDefault(uri);
            WebResponse response =  webRequest.GetResponse();
            XmlTextReader xmlr = new XmlTextReader(response.GetResponseStream());
            string ticket = "";
            string folderId = "0";
            bool foundTicket = false ;
            bool foundToken = false;
            string authToken = "";
            while (xmlr.Read())
            {
                if (foundTicket)
                {
                    ticket = xmlr.Value;
                    break;
                }
                if (xmlr.Name == "ticket")
                {
                    foundTicket = true;
                   
                }
                
            }
            if (ticket.Length != 0)
            {
                //This method is used in the authorization process. 
                //You should call this method after the user has authorized themself on box.net site. 
                //Pass ticket that you get when called get_ticket method. 
                //On a successful result, method will return 'get_auth_token_ok' as status, auth_token to use in other method calls, and user struct which describes logged user.
                //need to login from pc first
                //continue to authorise
                uri = new Uri(@"http://www.box.net/api/1.0/rest?action=get_auth_token&api_key=" + ApiKey + "&ticket=" + ticket);
                response = webRequest.GetResponse();
                if (response.ContentLength > 0)
                {
                    StreamReader sr = new StreamReader(response.GetResponseStream());
                    string sResponse = sr.ReadToEnd();
                    xmlr = new XmlTextReader(response.GetResponseStream());
                    while (xmlr.Read())
                    {
                        if (foundToken)
                        {
                            authToken = xmlr.Value;
                            break;
                        }
                        if (xmlr.Name == "auth_token")
                        {
                            foundToken = true;

                        }

                    }
                }
                else
                {
                    //not logged in - i think
                    throw new Exception("It's higly likely that you are not logged in");
                }
            }
            if (authToken.Length != 0)
            {
                //authorised
                //upload file
                //To upload a file, send HTTP POST request to http://upload.box.net/api/1.0/upload/<auth_token>/<folder_id>
                uri = new Uri("http://upload.box.net/api/1.0/upload/" + authToken + "/" + folderId);
                webRequest = WebRequest.CreateDefault(uri);
                webRequest.Method = "POST";
                webRequest.ContentType = "multipart/form-data";
                /*... <form action="http://upload.box.net/api/1.0/upload/0ef4e2b1be630554ffa4044c054b1954/756"
                         enctype="multipart/form-data" method="POST">
                        <input type="file" name="new_file1" />
                        <input type="file" name="new_file2" />
                        <input type="text" name="share" value="1" />
                        <input type="text" name="message" value="Hello" />
                        <input type="text" name="emails[]" value="test@domain.com" />
                        <input type="text" name="emails[]" value="test1@domain.com" />
                        <input type="submit" name="upload_files" value="Upload File" />
                       </form>*/
                //need to somehow add these inputs into the response....
                //see http://www.codeproject.com/KB/cs/multipart_request_C_.aspx
                
            }
            

        }
        
    }
}
